<?php

namespace App\Http\Controllers\Api\V1\Manager\Order;

use App\Exports\UniversalExport;
use App\Http\Controllers\Controller;
use App\Models\Level;
use App\Models\Order;
use App\Models\OrderDetail;
use App\Models\OrderNegotiationHistory;
use App\Models\OrderRetrun;
use App\Models\Supplier;
use App\Models\UserHome;
use App\Models\UserIncomeScoreLog;
use App\Models\UserTeamPriceLog;
use App\Services\Front\OrderService;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

class AfterOrderController extends Controller
{
    private $namespace = "App\Http\Controllers\Api\V1\Manager\Order\AfterOrderController";

    /**角色列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request)
    {
        $userId = $request->userId;
        $userName = $request->userName;
        $userPhone = $request->userPhone;
        $supplierName = $request->supplierName;
        $wuliu_num = $request->wuliu_num;
        $name = $request->name;
        $phone = $request->phone;
        $address = $request->address;
        $ordernum = $request->ordernum;
        $status = $request->status;
        $paytype = $request->paytype;
        $createStartTime = $request->createStartTime;
        $createEndTime = $request->createEndTime;
        $payStartTime = $request->payStartTime;
        $payEndTime = $request->payEndTime;
        $shouStartTime = $request->shouStartTime;
        $shouEndTime = $request->shouEndTime;
        $shouhou_wuliu_no = $request->shouhou_wuliu_no;
        $is_shouhou = $request->is_shouhou;
        $activityStatus = $request->activityStatus;
        $paycode = $request->paycode;
        $pageSize = $request->pageSize;
        $sql = OrderDetail::select('t0.id', 't1.id as orderId', 't1.ordernum', 't1.paycode', 't2.id as userId', 't2.nickname as userName', 't2.phone as userPhone', 't1.status', 't0.price', 't0.vip_price',
            't1.allprice', 't1.paytype', 't1.breakLevel', 't1.oBreak', 't1.wuliu_num', 't1.isVip', 't0.cost_price', 't0.pure_cost_price',
            't1.name', 't1.phone', 't1.address', 't3.id as supplierId', 't3.suppname as supplierName', 't1.supplier_cost', 't1.createtime as createTime', 't1.paytime as payTime', 't1.shouhuotime as shouhuoTime',
            't0.id as orderDetailId', 't0.goods_id', 't0.name as goods_name', 't0.image as goods_image', 't0.goods_spec_name', 't0.num as goods_num', 't0.is_shouhou', 't4.createTime as shouhouTime',
            \DB::raw('t0.realityFreight + t0.realityPrice as realityPrice'), 't0.realityFreight', 't1.activityStatus', 't1.break', 't1.oBreak', 't4.updateTime as shouhouUpdateTime','t4.return_price','t4.adminName')
            ->from((new OrderDetail())->getTable() . ' as t0')
            ->join((new Order())->getTable() . ' as t1', 't0.order_id', '=', 't1.id')
            ->leftJoin((new UserHome())->getTable() . ' as t2', 't1.userhome_id', '=', 't2.id')
            ->leftJoin((new Supplier())->getTable() . ' as t3', 't1.supplier_id', '=', 't3.id')
            ->join((new OrderRetrun())->getTable() . ' as t4', function ($query) {
                $query->on('t0.id', '=', 't4.orderdetail_id')
                    ->where('is_new', 1);
            })
            ->where('t1.is_integral', 0);
        $ids = $request->ids;
        $idList = [];
        if (isset($ids)) {
            $ids = explode(';', $ids);
            foreach ($ids as $v) {
                $idList[] = $v;
            }
        }
        if (isset($idList[0])) {
            $sql->whereIn('t0.id', $idList);
        }
        if (isset($is_shouhou)) {
            $sql->where('t0.is_shouhou', $is_shouhou);
        }
        if (isset($activityStatus)) {
            $sql->where('t1.activityStatus', $activityStatus);
        }
        if (isset($shouhou_wuliu_no)) {
            $sql->where('t4.shouhou_wuliu_no', 'like', "%$shouhou_wuliu_no%");
        }
        if (isset($createStartTime)) {
            $sql->where('t1.createtime', '>=', $createStartTime);
        }
        if (isset($createEndTime)) {
            $sql->where('t1.createtime', '<=', $createEndTime);
        }
        if (isset($payStartTime)) {
            $sql->where('t1.paytime', '>=', $payStartTime);
        }
        if (isset($payEndTime)) {
            $sql->where('t1.paytime', '<=', $payEndTime);
        }
        if (isset($shouStartTime)) {
            $sql->where('t1.shouhuotime', '>=', $shouStartTime);
        }
        if (isset($shouEndTime)) {
            $sql->where('t1.shouhuotime', '<=', $shouEndTime);
        }
        if (isset($userId)) {
            $sql->where('t1.userhome_id', $userId);
        }
        if (isset($userName)) {
            $sql->where('t2.nickname', 'like', "%$userName%");
        }
        if (isset($userPhone)) {
            $sql->where('t2.phone', 'like', "%$userPhone%");
        }
        if (isset($supplierName)) {
            $sql->where('t3.suppname', 'like', "%$supplierName%");
        }
        if (isset($wuliu_num)) {
            $sql->where('t1.wuliu_num', 'like', "%$wuliu_num%");
        }
        if (isset($name)) {
            $sql->where('t1.name', 'like', "%$name%");
        }
        if (isset($phone)) {
            $sql->where('t1.phone', 'like', "%$phone%");
        }
        if (isset($address)) {
            $sql->where('t1.address', 'like', "%$address%");
        }
        if (isset($ordernum)) {
            $sql->where('t1.ordernum', 'like', "%$ordernum%");
        }
        if (isset($status)) {
            $sql->where('t1.status', $status);
        }
        if (isset($paytype)) {
            $sql->where('t1.paytype', $paytype);
        }
        if (isset($paycode)) {
            $sql->where('t1.paycode', 'like', "%$paycode%");
        }
        if ($request->excel) {
            $orderList = $sql->orderByDesc('t4.createTime')
                ->get()
                ->toArray();
            $title = ['订单号', '支付单号', '用户id', '订单类型', '用户电话', '用户名称', '收件人名称', '收件人电话', '收件人地址', '订单状态', '商品名称', '规格属性', '商品数量', '商品单价',
                '商品总价', '运费', '实付款', '供应商名称', '含税成本单价', '含税成本总价', '不含税成本单价', '不含税成本总价', '物流单号', '物流备注内容', '支付方式', '订单创建时间', '订单支付时间', '确认收货时间', '订单备注',
                '售后状态', '售后发起时间', '售后处理时间', '实退金额', '操作人'];
            $data = [];
            $activityStatus = [0 => "未活动", 10 => "VIP订单", 30 => "每日爆款", 40 => "限时秒杀", 50 => "福利订单"];
            $status = [0 => '已取消', 1 => '待支付', 2 => '已支付', 3 => '已发货', 4 => '待评价', 5 => '已完成', 6 => '售后中', 9 => '已删除'];
            $payType = [1 => '微信', 2 => '支付宝', 3 => '立减券抵扣', 4 => '积分支付'];
            $is_shouhou_status = [0 => "未售后", 1 => "已申请售后", 2 => "已完成", 3 => "已拒绝"];
            foreach ($orderList as $order) {
                $statusType = $status[$order['status']];
//                $statusType = "未知状态";
//                if (($order['is_shouhou'] == 0) || ($order['is_shouhou'] == 3)) {
//                    $statusType = $status[$order['status']];
//                } else {
//                    if ($order['is_shouhou'] == 1) {
//                        $statusType = "售后中";
//                    }
//                    if ($order['is_shouhou'] == 2) {
//                        $statusType = "售后已完成";
//                    }
//                }
                $price = $order['price'];
                if ($order['activityStatus'] == 10 || $order['activityStatus'] == 30) {
                    $price = $order['vip_price'];
                }
                $detail = [];
                $detail[] = $order['ordernum'];
                $detail[] = $order['paycode'];
                $detail[] = $order['userId'];
                $detail[] = $activityStatus[$order['activityStatus']];
                $detail[] = $order['userPhone'];
                $detail[] = $order['userName'];
                $detail[] = $order['name'];
                $detail[] = $order['phone'];
                $detail[] = $order['address'];
                $detail[] = $statusType;
                $detail[] = $order['name'];
                $detail[] = $order['goods_spec_name'];
                $detail[] = $order['goods_num'];
                $detail[] = $price;
                $detail[] = $price * $order['goods_num'];
                $detail[] = $order['realityFreight'];
                $detail[] = $order['realityPrice'];
                $detail[] = $order['supplierName'];
                $detail[] = $order['cost_price'];
                $detail[] = $order['cost_price'] * $order['goods_num'];
                $detail[] = $order['pure_cost_price'];
                $detail[] = $order['pure_cost_price'] * $order['goods_num'];
                $detail[] = $order['wuliu_num'];
                $detail[] = $order['break'];
                if (isset($order['paytype']) && !empty($order['paytype'])) {
                    $detail[] = $payType[$order['paytype']];
                } else {
                    $detail[] = "未支付";
                }
                if (isset($order['createTime'])) {
                    $createTime = date("Y-m-d H:i:s", $order['createTime']);
                } else {
                    $createTime = null;
                }
                if (isset($order['payTime'])) {
                    $payTime = date("Y-m-d H:i:s", $order['payTime']);
                } else {
                    $payTime = null;
                }
                if (isset($order['shouhuoTime'])) {
                    $shouhuoTime = date("Y-m-d H:i:s", $order['shouhuoTime']);
                } else {
                    $shouhuoTime = null;
                }
                $detail[] = $createTime;
                $detail[] = $payTime;
                $detail[] = $shouhuoTime;
                $detail[] = $order['oBreak'];
                $detail[] = $is_shouhou_status[$order['is_shouhou']];
                $detail[] = $order['shouhouTime'];
                $detail[] = $order['shouhouUpdateTime'];
                $detail[] = $order['return_price'];
                $detail[] = $order['adminName'];
                $data[] = $detail;
            }
            return Excel::download(new UniversalExport($data, $title), "售后订单" . date("ymdHis") . '.xlsx');
        }
        $orderList = $sql->orderByDesc('t4.createTime')
            ->paginate($pageSize)
            ->toArray();
        $data['orderList'] = $orderList['data'];
        foreach ($data['orderList'] as $key => $value) {
            if (isset($value['createTime'])) {
                $data['orderList'][$key]['createTime'] = date("Y-m-d H:i:s", $value['createTime']);
            }
            if (isset($value['payTime'])) {
                $data['orderList'][$key]['payTime'] = date("Y-m-d H:i:s", $value['payTime']);
            }
            if (isset($value['shouhuoTime'])) {
                $data['orderList'][$key]['shouhuoTime'] = date("Y-m-d H:i:s", $value['shouhuoTime']);
            }
        }
        $data['totalNum'] = $orderList['total'];
        return $this->apiResponse($data);
    }

    public function show(Request $request, $orderDetailId)
    {
        $orderDetail = OrderDetail::select('t1.id as orderdetail_id', 't2.id as order_id', 't2.ordernum', 't2.paycode', 't1.is_shouhou', 't1.image as goodimage', 't1.name as goodname',
            't1.goods_spec_name', 't1.num as goodnum', 't1.price', 't1.price as totalPrice', 't1.realityFreight', 't1.realityPrice', 't1.tkprice', 't2.createtime', 't2.paytype', 't1.cost_price', 't1.cost_price as total_cost_price',
            't1.shuilv', 't2.paytime', 't2.shouhuotime', 't2.wuliu_code', 't2.wuliu_code', 't2.activityStatus',
            't3.id as userId', 't3.nickname as userName', 't3.phone as userPhone', 't2.paytype', 't2.name', 't2.phone', 't2.address', 't2.wuliu_num', 't1.break', 't2.isVip', 't1.vip_price')
            ->from((new OrderDetail())->getTable() . ' as t1')
            ->join((new Order())->getTable() . ' as t2', 't1.order_id', '=', 't2.id')
            ->leftJoin((new UserHome())->getTable() . ' as t3', 't2.userhome_id', '=', 't3.id')
            ->where('t1.id', $orderDetailId)
            ->first();
        if (isset($orderDetail['goodnum']) && !empty($orderDetail['goodnum']) && isset($orderDetail['price']) && !empty($orderDetail['price'])) {
            $orderDetail['totalPrice'] = $orderDetail['goodnum'] * $orderDetail['price'];
        }
        if (isset($orderDetail['goodnum']) && !empty($orderDetail['goodnum']) && isset($orderDetail['cost_price']) && !empty($orderDetail['cost_price'])) {
            $orderDetail['total_cost_price'] = $orderDetail['goodnum'] * $orderDetail['cost_price'];
        }
        $orderReturn = OrderRetrun::where('orderdetail_id', $orderDetailId)
            ->orderByDesc('is_new')
            ->orderByDesc('createTime')
            ->get()
            ->toArray();
        $shouhouTime = null;
        foreach ($orderReturn as $key => $value) {
            if (!$shouhouTime) {
                $shouhouTime = $value['createTime'];
            }
            if (isset($value['image']) && !empty($value['image'])) {
                unset($k);
                unset($v);
                $image = explode(';', $value['image']);
                $return_image_list = [];
                foreach ($image as $k => $v) {
                    $return_image_list[] = path($v);
                }
                $orderReturn[$key]['image'] = $return_image_list;
            }
            if (isset($value['service_image']) && !empty($value['service_image'])) {
                unset($k);
                unset($v);
                $image = explode(';', $value['service_image']);
                $service_image_list = [];
                foreach ($image as $k => $v) {
                    $service_image_list[] = path($v);
                }
                $orderReturn[$key]['service_image'] = $service_image_list;
                $orderReturn[$key]['tkprice'] = $orderDetail['tkprice'];
            }
        }
        $orderDetail['shouhouTime'] = $shouhouTime;
        $orderDetail['orderReturn'] = $orderReturn;
        return $this->apiResponse($orderDetail);
    }

    public function negotiationHistory(Request $request, $orderReturnId)
    {
        if (!$this->permissionAuthenticate($request->RESOURCE, "这里输入需要的资源组权限")) {
            return $this->apiResponse('', config('errorLiu.NINZHANWUGAIJIEKOUQUANX'));
        }
        $negotiationList = OrderNegotiationHistory::where('order_return_id', $orderReturnId)->orderBy('createTime', "desc")->get()->toArray();
        $data = [];
        $data['negotiationList'] = $negotiationList;
        $data['totalNum'] = count($negotiationList);
        return $this->apiResponse($data);
    }

    public function upBreak(Request $request)
    {
        if (!$this->permissionAuthenticate($request->RESOURCE, "这里输入需要的资源组权限")) {
            return $this->apiResponse('', config('errorLiu.NINZHANWUGAIJIEKOUQUANX'));
        }
        $orderdetail_id = $request->orderdetail_id;
        $break = $request->break;
        OrderDetail::where("id", $orderdetail_id)->update(['break' => $break]);
        //添加管理员日志开始
        $url = $this->namespace . "/upBreak";
        $title = "修改";
        $content = "为id为【" . $orderdetail_id . "】的子订单修改售后备注,备注为:【" . $break . "】";
        $this->addAdminLog($request->ADMINID, $request->ADMINUSERNAME, $url, "售后订单模块", $title, $content, $request->getClientIp());
        //添加管理员日志结束
        return $this->apiResponse($break);
    }

    //拒绝售后
    public function refundNew(Request $request)
    {
        if (!$this->permissionAuthenticate($request->RESOURCE, "这里输入需要的资源组权限")) {
            return $this->apiResponse('', config('errorLiu.NINZHANWUGAIJIEKOUQUANX'));
        }
        $id = $request->orderreturn_id;
        $service_break = $request->service_break;
        $service_image = $request->service_image;
        if (!$service_break || empty($service_break) || !$id || empty($id)) {
            return $this->apiResponse('', config('errorLiu.NOTCANSHUID'));
        }
        $shouhou_status = 2;
        $orderReturn = OrderRetrun::where('id', $id)->first();
        if (!$orderReturn['is_new']) {
            return $this->apiResponse('', config('errorLiu.GAISHOUJILUBUSHIZUIXINDE'));
        }
        if ($orderReturn['shouhou_status'] && $orderReturn['schedule'] != "RETERUN") {
            return $this->apiResponse('', config('errorLiu.SHOUOUUJILYIJINGCHUL'));
        }
        $order = Order::where('id', $orderReturn['order_id'])->first();
        if ($order['status'] == 5 || $order['status'] == 4) {
            return $this->apiResponse('', config('errorLiu.GAIDINGDANYIUJIWANCHENG'));
        }
        $orderDetail = OrderDetail::where('id', $orderReturn['orderdetail_id'])->first();
        if ($orderDetail['is_shouhou'] != 1) {
            return $this->apiResponse('', config('errorLiu.SHOUOUUJILYIJINGCHUL'));
        }
        try {
            \DB::beginTransaction();
            OrderRetrun::where('id', $id)->update(['shouhou_status' => 2, 'schedule' => "COMPELETE", 'service_image' => null, 'service_break' => $service_break, 'updateTime' => date("Y-m-d H:i:s"), 'adminId' => $request->ADMINID, 'adminName' => $request->ADMINUSERNAME]);
            OrderDetail::where('id', $orderReturn['orderdetail_id'])->update(['is_shouhou' => 3]);
            $orderDetailNum = OrderDetail::where('order_id', $order['id'])->whereRaw('is_shouhou = 1')->count();
            if (!$orderDetailNum) {
                Order::where('id', $order['id'])->update(['is_shouhou' => 0]);
            }
            if ($order['status'] == 6) {
                Order::where('id', $order['id'])->update(['status' => $order['oldStatus']]);
            }
            $orderService = new OrderService();
            $levelList = Level::get()->toArray();
            $orderService->integralEnter($order, $levelList);
            //return var_dump($orderService->integralEnter($order,$levelList));
            $content = "商家拒绝退款";
            $order_negotiation_history = [
                'order_id' => $orderReturn['order_id'],
                'orderdetail_id' => $orderReturn['orderdetail_id'],
                'order_return_id' => $orderReturn['id'],
                'content' => $content,
                'image' => null,
                'break' => $service_break,
                'shouhou_status' => $shouhou_status,
                'createTime' => date("y-m-d H:i:s")
            ];
            OrderNegotiationHistory::insert($order_negotiation_history);
            //添加管理员日志开始
            $url = $this->namespace . "/refundNew";
            $title = "修改";
            $content = "拒绝了id为【" . $orderReturn['orderdetail_id'] . "】的子订单的售后申请,理由为:【" . $service_break . "】";
            $this->addAdminLog($request->ADMINID, $request->ADMINUSERNAME, $url, "售后订单模块", $title, $content, $request->getClientIp());
            //添加管理员日志结束
            \DB::commit();
            return $this->apiResponse(true);
        } catch (\Exception $e) {
            \DB::rollback();
            return $this->apiResponse('', config('errorLiu.CAHAOZUOSHIBAISHAOHOU'));
        }
    }

    //退款
    public function agressdNew(Request $request)
    {
        if (!$this->permissionAuthenticate($request->RESOURCE, "这里输入需要的资源组权限")) {
            return $this->apiResponse('', config('errorLiu.NINZHANWUGAIJIEKOUQUANX'));
        }
        $id = $request->orderreturn_id;
        $sjtkprice = $request->sjtkprice;
        if (!$sjtkprice || empty($sjtkprice) || !$id || empty($id)) {
            return $this->apiResponse('', config('errorLiu.NOTCANSHUID'));
        }
        $shouhou_status = 1;
        $orderReturn = OrderRetrun::where('id', $id)->first();

        if (!$orderReturn['is_new']) {
            return $this->apiResponse('', config('errorLiu.GAISHOUJILUBUSHIZUIXINDE'));
        }
        if ($orderReturn['return_type'] != "HAM") {
            if ($orderReturn['shouhou_status']) {
                return $this->apiResponse('', config('errorLiu.SHOUOUUJILYIJINGCHUL'));
            }
        } else {
            if (!isset($orderReturn['shouhou_wuliu_no']) && empty($orderReturn['shouhou_wuliu_no'])) {
                return $this->apiResponse('', config('errorLiu.GAISHOUHOUJIWEITIANXIEHUIZHIDAN'));
            }
            if ($orderReturn['shouhou_status'] != 1) {
                return $this->apiResponse('', config('errorLiu.GAITUIHUOTUIIOKMUANJILUOBINGFEI'));
            }
            if ($orderReturn['schedule'] != "RETERUN") {
                return $this->apiResponse('', config('errorLiu.GAITUIHNHUIBINGFEIYUIHUOJINDU'));
            }
        }
        $order = Order::where('id', $orderReturn['order_id'])->first();
        if ($order['status'] == 5 || $order['status'] == 4) {
            return $this->apiResponse('', config('errorLiu.GAIDINGDANYIUJIWANCHENG'));
        }
        $orderDetail = OrderDetail::where('id', $orderReturn['orderdetail_id'])->first();
        if ($orderDetail['is_shouhou'] != 1) {
            return $this->apiResponse('', config('errorLiu.SHOUOUUJILYIJINGCHUL'));
        }
        if (intval(strval($sjtkprice * 100)) > intval(strval($orderDetail['tkprice'] * 100))) {
            return $this->apiResponse('', config('errorLiu.TUIKUANIMNEWUFADAYUYIJIJIN'));
        }
        //退款
        try {
            \DB::beginTransaction();
            $orderService = new OrderService();
            $status = $orderService->refund($sjtkprice, $order, $orderDetail);
            if ($status < 1) {
                throw new \Exception("退款失败,请稍后重试");
            }
            UserHome::where('id', $order['userhome_id'])->increment('spend', -$sjtkprice);
            //Order::where('id', $order['id'])->increment('allprice', -$sjtkprice);
            OrderRetrun::where('id', $id)->update(['shouhou_status' => 1, 'schedule' => "COMPELETE", 'return_price' => $sjtkprice, 'service_image' => null, 'service_break' => null, 'updateTime' => date("Y-m-d H:i:s"), 'adminId' => $request->ADMINID, 'adminName' => $request->ADMINUSERNAME]);
            $content = "商家已同意退款";
            $order_negotiation_history = [
                'order_id' => $orderReturn['order_id'],
                'orderdetail_id' => $orderReturn['orderdetail_id'],
                'order_return_id' => $orderReturn['id'],
                'content' => $content,
                'image' => null,
                'break' => null,
                'shouhou_status' => $shouhou_status,
                'createTime' => date("y-m-d H:i:s")
            ];
            OrderNegotiationHistory::insert($order_negotiation_history);
            UserIncomeScoreLog::where('order_detail_id', $orderReturn['orderdetail_id'])->update(['status' => "PAST"]);
            UserTeamPriceLog::where('order_detail_id', $orderReturn['orderdetail_id'])->update(['status' => "PAST"]);
            //添加管理员日志开始
            $url = $this->namespace . "/agressdNew";
            $title = "修改";
            $content = "为id为【" . $orderReturn['orderdetail_id'] . "】的子订单的售后申请退款,退款金额为:【" . $sjtkprice . "】";
            $this->addAdminLog($request->ADMINID, $request->ADMINUSERNAME, $url, "售后订单模块", $title, $content, $request->getClientIp());
            //添加管理员日志结束
            \DB::commit();
            return $this->apiResponse(true);
        } catch (\Exception $e) {
            \DB::rollback();
            return $this->apiResponse('', config('errorLiu.CAHAOZUOSHIBAISHAOHOU'));
        }
    }

    //同意退货
    public function rechanging(Request $request)
    {
        if (!$this->permissionAuthenticate($request->RESOURCE, "这里输入需要的资源组权限")) {
            return $this->apiResponse('', config('errorLiu.NINZHANWUGAIJIEKOUQUANX'));
        }
        $id = $request->orderreturn_id;
        if (!$id || empty($id)) {
            return $this->apiResponse('', config('errorLiu.NOTCANSHUID'));
        }
        $shouhou_status = 1;
        $orderReturn = OrderRetrun::where('id', $id)->first();
        if (!$orderReturn['is_new']) {
            return $this->apiResponse('', config('errorLiu.GAISHOUJILUBUSHIZUIXINDE'));
        }
        if ($orderReturn['shouhou_status']) {
            return $this->apiResponse('', config('errorLiu.SHOUOUUJILYIJINGCHUL'));
        }
        $order = Order::where('id', $orderReturn['order_id'])->first();
        if ($order['status'] == 5 || $order['status'] == 4) {
            return $this->apiResponse('', config('errorLiu.GAIDINGDANYIUJIWANCHENG'));
        }
        $orderDetail = OrderDetail::where('id', $orderReturn['orderdetail_id'])->first();
        if ($orderDetail['is_shouhou'] != 1) {
            return $this->apiResponse('', config('errorLiu.SHOUOUUJILYIJINGCHUL'));
        }
        try {
            \DB::beginTransaction();
            if ($orderReturn['return_type'] == "HAM") {
                //退货退款
                OrderRetrun::where('id', $id)
                    ->update(['shouhou_status' => 1, 'schedule' => "RETERUN", 'service_image' => null, 'service_break' => null, 'updateTime' => date("Y-m-d H:i:s"), 'adminId' => $request->ADMINID, 'adminName' => $request->ADMINUSERNAME]);
                $content = "商家已同意退货退款,请尽快退货,退货地址请联系客服";
            } else {
                return $this->apiResponse('', config('errorLiu.GAISHOUHHOUBUSHITUIHUOTUIK'));
            }
            $order_negotiation_history = [
                'order_id' => $orderReturn['order_id'],
                'orderdetail_id' => $orderReturn['orderdetail_id'],
                'order_return_id' => $orderReturn['id'],
                'content' => $content,
                'image' => null,
                'break' => null,
                'shouhou_status' => $shouhou_status,
                'createTime' => date("y-m-d H:i:s")
            ];
            OrderNegotiationHistory::insert($order_negotiation_history);
            //添加管理员日志开始
            $url = $this->namespace . "/rechanging";
            $title = "修改";
            $content = "同意了id为【" . $orderReturn['orderdetail_id'] . "】的子订单的售后退货申请";
            $this->addAdminLog($request->ADMINID, $request->ADMINUSERNAME, $url, "售后订单模块", $title, $content, $request->getClientIp());
            //添加管理员日志结束
            \DB::commit();
            return $this->apiResponse(true);
        } catch (\Exception $e) {
            \DB::rollback();
            return $this->apiResponse('', config('errorLiu.CAHAOZUOSHIBAISHAOHOU'));
        }
    }
}
